package cn.tedu.tea.service;

import cn.tedu.tea.domain.OrderAddress;
import cn.tedu.tea.domain.param.OrderAddressAddNewDTO;
import cn.tedu.tea.domain.param.OrderAddressUpdateDTO;
import cn.tedu.tea.domain.vo.OrderAddressListVO;
import cn.tedu.tea.domain.vo.OrderAddressStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.OrderAddressMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderAddressServiceImpl implements IOrderAddressService {

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Override
    public void addNew(OrderAddressAddNewDTO orderAddressAddNewDTO) {
        log.debug("开始处理【添加订单地址】的业务，参数： {}", orderAddressAddNewDTO);
        int count = orderAddressMapper.countByOrderNo(orderAddressAddNewDTO.getOrderNo());
        if (count > 0){
            String message = "添加订单地址失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(orderAddressAddNewDTO, orderAddress);
        int rows = orderAddressMapper.insert(orderAddress);
        if (rows != 1){
            String message = "添加订单地址失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除订单地址】的业务，参数： {}", id);
        OrderAddressStandardVO orderAddressStandard = orderAddressMapper.getStandardById(id);
        if (orderAddressStandard == null){
            String message = "删除订单地址失败，要删除的订单地址不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = orderAddressMapper.deleteById(id);
        if (rows != 1){
            String message = "删除订单地址失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除订单地址】的业务，参数： {}", ids);
        int rows = orderAddressMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除订单地址失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(OrderAddressUpdateDTO orderAddressUpdateDTO) {
        log.debug("开始处理【修改订单地址信息】的业务，参数：{}", orderAddressUpdateDTO);
        OrderAddressStandardVO orderAddressStandard = orderAddressMapper.getStandardById(orderAddressUpdateDTO.getId());
        if (orderAddressStandard == null){
            String message = "修改订单地址失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = orderAddressMapper.countByOrderNoAndNotId(orderAddressUpdateDTO.getOrderNo(), orderAddressUpdateDTO.getId());
        if (count > 0){
            String message = "修改订单地址失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(orderAddressUpdateDTO, orderAddress);
        int rows = orderAddressMapper.update(orderAddress);
        if (rows != 1){
            String message = "修改订单地址失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public OrderAddressStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询订单地址详情】的业务，参数： {}", id);
        OrderAddressStandardVO orderAddressStandard = orderAddressMapper.getStandardById(id);
        if (orderAddressStandard == null){
            String message = "查询订单地址详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return orderAddressStandard;
    }

    @Override
    public List<OrderAddressListVO> list() {
        log.debug("开始处理【查询订单地址列表】的业务，无参数");
        List<OrderAddressListVO> list = orderAddressMapper.list();
        return list;
    }
    
}
